{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ffef8d85-6a44-4e54-8b99-df99f63e3d6c",
   "metadata": {},
   "source": [
    "# Data play\n",
    "\n",
    "This is a simple example on how to use (_still largely incomplete!_) GRANDlib classes for analysis-oriented data readout.\n",
    "\n",
    "First, let's generate a ROOT file with random, dummy events. The old file will be cleared if exists. This uses an example script examples/io/DataStoringExample.py. Please be sure to restart kernel each time you try this script.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1550fe85-e21d-4423-8884-b8d90cb96af7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os.path\n",
    "\n",
    "if os.path.isfile(\"dummy_data.root\"):\n",
    "    os.remove(\"dummy_data.root\")\n",
    "%run ../io/DataStoringExample.py dummy_data.root"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "80d632ad-3633-4516-a4d5-b83dc1378f6d",
   "metadata": {},
   "source": [
    "Now we import the necessary python modules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c65292a-a602-4ce1-8ba1-94e4a7b11c01",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "from grand.grandlib_classes.grandlib_classes import *"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbc5931a-6774-48ab-b67d-6564e9def0f2",
   "metadata": {},
   "source": [
    "Let's create an Event, and provide the file name, run and event numbers (which we know to exist) for it"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b76571c5-7578-4a45-a86c-a612c996d552",
   "metadata": {},
   "outputs": [],
   "source": [
    "e = Event()\n",
    "e.file = \"dummy_data.root\"\n",
    "e.run_number = 0\n",
    "e.event_number = 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02d7717a-9877-4092-a850-350e450240a2",
   "metadata": {},
   "source": [
    "The Event has to be told to initialise its contents from the provided file, with provided run and event numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "198b608d-648d-4af3-868b-a7732708ea2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "e.fill_event_from_trees()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea02419d-a9f6-4d15-864a-4b9e80671e0b",
   "metadata": {},
   "source": [
    "Let's print a Run property (constant for the whole run) - the data source. The default value is \"other\". So if it prints out different, we know that it has been initailised from TTrees:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "08d5a612-4a2c-4e2b-86fd-210ba8eb413f",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(e.data_source)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31dca62b-c431-40ea-ae19-86f82e6f67cc",
   "metadata": {},
   "source": [
    "How many traces do we have in this event?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da680551-7907-4549-9d06-224c0cdfed01",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(len(e.voltages))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cda0986-7299-439b-b4a9-31d37bbf3d60",
   "metadata": {},
   "source": [
    "What are the contents of the first trace X?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "44fdbd33-5ffb-4509-af36-23f4f5d9c8b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(e.voltages[0].trace_x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a16053b1-82ef-4e2e-965c-184fa1597f0b",
   "metadata": {},
   "source": [
    "Let's draw similar Efield trace with ROOT:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83d6ca0a-f0d4-495f-beaa-d517aa479bf6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ROOT\n",
    "c = ROOT.TCanvas(\"c\", \"c\", 1000, 500)\n",
    "c.SetLeftMargin(0.15)\n",
    "trace_len = len(e.efields[0].trace_x)\n",
    "g = ROOT.TGraph(trace_len, np.arange(trace_len).astype(np.float64), np.array(e.voltages[0].trace_x).astype(np.float64))\n",
    "g.GetXaxis().SetTitle(\"Time ;)\")\n",
    "g.GetYaxis().SetTitle(\"Efield [C]\")\n",
    "g.Draw(\"AL\")\n",
    "c.Draw()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88df5385-1d46-40f9-9c7b-6b953353f9ee",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
